package com.whiteape.book.ui.activity;

import android.os.Build;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.whiteape.book.R;
import com.whiteape.book.WaApp;
import com.whiteape.book.contract.BookCategoryContract;
import com.whiteape.book.model.zssq.BookSubSortBean;
import com.whiteape.book.model.zssq.packages.BookSortPackage;
import com.whiteape.book.model.zssq.packages.BookSubSortPackage;
import com.whiteape.book.presenter.BookCategoryPresenter;
import com.whiteape.book.ui.adapter.BookSortAdapter;
import com.whiteape.book.ui.base.BaseMVPActivity;
import com.whiteape.book.widget.RefreshLayout;
import com.whiteape.book.widget.itemdecoration.DividerGridItemDecoration;

import butterknife.BindView;

public class BookCategoryActivity extends BaseMVPActivity<BookCategoryContract.Presenter> implements BookCategoryContract.View{
    private static final int SPAN_COUNT = 3;

    @BindView(R.id.book_sort_rl_refresh)
    RefreshLayout mRlRefresh;
    @BindView(R.id.book_sort_rv_boy)
    RecyclerView mRvBoy;
    @BindView(R.id.book_sort_rv_girl)
    RecyclerView mRvGirl;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_male)
    TextView tvMale;
    @BindView(R.id.tv_female)
    TextView tvFemale;

    private BookSortAdapter mBoyAdapter;
    private BookSortAdapter mGirlAdapter;

    private BookSubSortPackage mSubSortPackage;

    @Override
    protected BookCategoryContract.Presenter bindPresenter() {
        return new BookCategoryPresenter();
    }

    @Override
    public void showError() {

    }

    @Override
    protected int getContentId() {
        return R.layout.activity_book_category;
    }

    @Override
    protected void initView() {
        super.initView();
//        getWindow().getDecorView().setBackgroundColor(ThemeStore.backgroundColor(this));
        setUpAdapter();
//        getWindow().getDecorView().setBackgroundColor(WaApp.getInstance().getResources().getColor(R.color.statusBarColor));
        setupTextColor();
    }

    private void setupTextColor(){
//        tvMale.setTextColor(ThemeStore.accentColor(this));
//        tvFemale.setTextColor(ThemeStore.accentColor(this));
        tvMale.setTextColor(WaApp.getInstance().getResources().getColor(R.color.colorAccent));
        tvFemale.setTextColor(WaApp.getInstance().getResources().getColor(R.color.colorAccent));
    }


    @Override
    protected void startLogic() {
        super.startLogic();
        mRlRefresh.showLoading();
        mPresenter.refreshSortBean();
    }

    @Override
    protected void setUpToolbar(Toolbar toolbar) {
        super.setUpToolbar(toolbar);
        setSupportActionBar(toolbar);
        toolbar.setNavigationOnClickListener((v)->finish());

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle(R.string.category);
        }

    }

    private void setUpAdapter(){
        mBoyAdapter = new BookSortAdapter();
        mGirlAdapter = new BookSortAdapter();

        mRvBoy.setLayoutManager(new GridLayoutManager(this,SPAN_COUNT));
        mRvGirl.setLayoutManager(new GridLayoutManager(this,SPAN_COUNT));
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            RecyclerView.ItemDecoration itemDecoration = new DividerGridItemDecoration(this, R.drawable.shape_divider_row, R.drawable.shape_divider_col);
            mRvBoy.addItemDecoration(itemDecoration);
            mRvGirl.addItemDecoration(itemDecoration);
        }

        mRvBoy.setAdapter(mBoyAdapter);

        mRvGirl.setAdapter(mGirlAdapter);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);

    }

    @Override
    protected void initEvent() {
        super.initEvent();
        initClick();
    }

    private void initClick(){
        mBoyAdapter.setOnItemClickListener(
                (view,pos) -> {
                    BookSubSortBean subSortBean = mSubSortPackage.getMale().get(pos);
                    //上传
                    BookCategoryDetailActivity.startActivity(this,"male",subSortBean);
                }
        );
        mGirlAdapter.setOnItemClickListener(
                (view,pos) -> {
                    BookSubSortBean subSortBean = mSubSortPackage.getFemale().get(pos);
                    //上传
                    BookCategoryDetailActivity.startActivity(this,"female",subSortBean);
                }
        );
    }


    @Override
    public void finishRefresh(BookSortPackage sortPackage, BookSubSortPackage subSortPackage) {
        if (sortPackage == null || sortPackage.getMale().size() == 0 || sortPackage.getFemale().size() == 0){
            mRlRefresh.showEmpty();
        }
        else {
            mBoyAdapter.refreshItems(sortPackage.getMale());
            mGirlAdapter.refreshItems(sortPackage.getFemale());
        }
        mSubSortPackage = subSortPackage;
    }

    @Override
    public void showLoadError() {

        mRlRefresh.showError();
    }

    @Override
    public void complete() {

        mRlRefresh.showFinish();
    }
}
